package com.lzj.jdbc;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HelloHBaseDDL {
    //获取HBase管理员类
    private Admin admin;
    //获取表信息
    private Table table;
    //获取数据库连接
    private Connection connection;

    /**
     * 命名空间
     *
     * @throws IOException
     */
    @Test
    public void createNameSpace() throws IOException {
        NamespaceDescriptor mkNameSpace = NamespaceDescriptor.create("lzj").build();
        this.admin.createNamespace(mkNameSpace);
        System.out.println("HelloHBase.createNameSpace[表空间创建完成]");
    }

    /**
     * 创建单列族的表
     *
     * @throws IOException
     */
    @Test
    public void createOneColumnFamilyTable() throws IOException {
        TableDescriptorBuilder table = TableDescriptorBuilder.newBuilder(TableName.valueOf("lzj:teacher"));
        ColumnFamilyDescriptorBuilder columnBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("info"));
        ColumnFamilyDescriptor familyDescriptor = columnBuilder.build();
        table.setColumnFamily(familyDescriptor);
        admin.createTable(table.build());
    }

    /**
     * 创建多列族的表
     *
     * @throws IOException
     */
    @Test
    public void createMultiPartColumnFamilyTable() throws IOException {
        TableDescriptorBuilder table = TableDescriptorBuilder.newBuilder(TableName.valueOf("lzj:student"));
        ColumnFamilyDescriptorBuilder infoCF = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("info"));
        ColumnFamilyDescriptorBuilder scoreCF = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("score"));
        List<ColumnFamilyDescriptor> columnFamilyDescriptors = new ArrayList<>();
        columnFamilyDescriptors.add(infoCF.build());
        columnFamilyDescriptors.add(scoreCF.build());
        table.setColumnFamilies(columnFamilyDescriptors);
        admin.createTable(table.build());
    }

    /**
     * 查询所有表的信息
     *
     * @throws IOException
     */
    @Test
    public void listTables() throws IOException {
        TableName[] tableNames = admin.listTableNames();
        for (TableName tableName : tableNames) {
            System.out.println(tableName);
        }
    }

    /**
     * 列出指定命名空间的表信息
     *
     * @throws IOException
     */
    @Test
    public void listTablesByNameSpace() throws IOException {
        TableName[] tableNames = admin.listTableNamesByNamespace("lzj");
        for (TableName tableName : tableNames) {
            System.out.println(tableName);
        }
    }

    @Before
    public void init() throws IOException {
        Configuration configuration = HBaseConfiguration.create();
//        configuration.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181"); // 替换为实际地址

        this.connection = ConnectionFactory.createConnection(configuration);
        this.admin = connection.getAdmin();
        //获取数据库表信息
        table = this.connection.getTable(TableName.valueOf("lzj:student"));
    }

    @After
    public void destory() throws IOException {
        if (admin != null) {
            admin.close();
        }
        if (table != null) {
            table.close();
        }
        if (connection != null) {
            connection.close();
        }
    }

}
